package com.example.planschedulingsystem.controller;

import com.example.planschedulingsystem.entity.Plane;
import com.example.planschedulingsystem.entity.PlaneTrip;
import com.example.planschedulingsystem.entity.User;
import com.example.planschedulingsystem.module.UserAccount;
import com.example.planschedulingsystem.service.PlaneManagerService;
import com.example.planschedulingsystem.service.PlaneTripManagerService;
import com.example.planschedulingsystem.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = {"/api"})
@CrossOrigin("*")
public class PlaneController {
    @Autowired
    private PlaneManagerService planeManagerService;

    @Autowired
    private PlaneTripManagerService planeTripManagerService;

    @Autowired
    private UserManagerService userManagerService;

    @PostMapping(value = "/login")
    public User login(@RequestBody UserAccount user) {
        return userManagerService.getUser(user.getPassword(), user.getUsername());
    }

    @PostMapping(value = "/add/plane")
    public Plane addPlane(@RequestBody Plane plane) {
        return planeManagerService.save(plane);
    }

    @GetMapping(value = "/get/all/plane")
    public List<Plane> getAllPlane() {
        return planeManagerService.findAll();
    }

    @PostMapping(value="/add/trip")
    public PlaneTrip addTrip(@RequestBody PlaneTrip trip) {
        return planeTripManagerService.save(trip);
    }

    @GetMapping(value = "/get/planeTripsById")
    public List<PlaneTrip> getPlaneTripsById(@RequestParam Long planeId) {
        return planeTripManagerService.findAllByPlaneId(planeId);
    }
    @GetMapping(value = "/get/all/planeTrips")
    public List<PlaneTrip> getAllPlaneTrips() {
        return planeTripManagerService.findAllByDeparturetimeAfterAndDeparturetimeBefore();
    }

    @PostMapping(value = "/post/saveOrUpdate/planeTrips")
    public PlaneTrip postSaveOrUpdatePlaneTrips(@RequestBody PlaneTrip trip) {
        return planeTripManagerService.saveOrUpdate(trip);
    }

    @PostMapping(value = "/post/delete/planeTrip")
    public Long postDeletePlaneTrip(@RequestBody PlaneTrip trip) {
        return planeTripManagerService.delete(trip.getID());
    }

    @PostMapping(value = "/post/delete/plane")
    public Long postDeletePlane(@RequestBody Plane plane) {
        return planeManagerService.delete(plane);
    }
}
